﻿using KinonekoSoftware.CodariaDB.IO.Formats;
using KinonekoSoftware.Platform.Windows;
using FileFormat = KinonekoSoftware.CodariaDB.IO.Formats.FileFormat;
using XplatFileFormat = KinonekoSoftware.Foundation.Platform.FileFormat;

// ReSharper disable JoinDeclarationAndInitializer

namespace KinonekoSoftware.Studio
{
    partial class Platform
    {
        public static async Task Write<T>(T instance, string name, Func<T, byte[]> converter = null) where T : class
        {
            string fileName;

#if WINDOWS
            var opendlg = Shell.Save("问卷模板文件|*.sst", "*.sst", name);

            if (opendlg.ShowDialog() != true)
            {
                return;
            }

            fileName = opendlg.FileName;
#else
            var ss  = Ioc.Get<IStorageService>();
            var dir = ss.GetSpecialFolder(SpecialFolder.Picture);
            fileName = Path.Combine(dir, $"{name}.sst");
#endif
            converter ??= x => JSON.Dump(x);
            await File.WriteAllBytesAsync(fileName, converter(instance));
        }

        public static async Task<T> Read<T>() where T : class
        {
            
            byte[] buffer;
#if WINDOWS
            var opendlg = Shell.Open("问卷模板文件|*.sst");

            if (opendlg.ShowDialog() != true)
            {
                return default(T);
            }

            buffer = await File.ReadAllBytesAsync(opendlg.FileName);
#else
            var ss      = Ioc.Get<IStorageService>();
            var opendlg = await ss.SingleFile(XplatFileFormat.All);

            if (!opendlg.IsFinished)
            {
                return default(T);
            }

            buffer = opendlg.Value.Buffer;
#endif
            return JSON.File<T>(buffer);
        }
        
        public static async Task WriteText<T>(T instance, string name, uint version, uint type) where T : class
        {

            string fileName;

#if WINDOWS
            var opendlg = Shell.Save("问卷模板文件|*.sst", "*.sst", name);

            if (opendlg.ShowDialog() != true)
            {
                return;
            }

            fileName = opendlg.FileName;
#else
            var ss = Ioc.Get<IStorageService>();
            var dir = ss.GetSpecialFolder(SpecialFolder.Picture);
            fileName = Path.Combine(dir, $"{name}.sst");
#endif
            //
            //
            await CodariaDB.IO.Formats.FileFormat.WriteText<T>(fileName, version, type, instance);
        }

        public static async Task<Result<TextFormat<T>>> ReadText<T>(uint version, uint type) where T : class
        {
            byte[] buffer;
#if WINDOWS
            var opendlg = Shell.Open("问卷模板文件|*.sst");

            if (opendlg.ShowDialog() != true)
            {
                return Result<TextFormat<T>>.Failure;
            }

            buffer = await File.ReadAllBytesAsync(opendlg.FileName);
#else
            var ss = Ioc.Get<IStorageService>();
            var opendlg = await ss.SingleFile(XplatFileFormat.All);

            if (!opendlg.IsFinished)
            {
                    return Result<TextFormat<T>>.Failure;
            }

            buffer = opendlg.Value.Buffer;
#endif
            return FileFormat.ReadText<T>(buffer, version, type);
        }
    }
}